package joe.beer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddNewBeerActivity extends Activity {

	EditText beernameText;
	EditText breweryNameText;
	EditText abvText;
	Button addButton;
	MySQLiteHelper dbHelper;
	
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addnewbeer);
		
		beernameText = (EditText) findViewById(R.id.beerNameText);
		breweryNameText = (EditText) findViewById(R.id.breweryNameText);
		abvText = (EditText) findViewById(R.id.abvText);
		addButton = (Button) findViewById(R.id.buttonAdd);
		
		addButton.setOnClickListener(l);
		
		dbHelper = new MySQLiteHelper(this);
	}
	
	private OnClickListener l = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};
	
	@Override
	public void finish() 
	{
		String beerName = beernameText.getText().toString();
		String breweryName = breweryNameText.getText().toString();
		String abv = abvText.getText().toString();
		if (abv.isEmpty() || beerName.isEmpty())
		{
			AlertDialog ad = new AlertDialog.Builder(this).create();
			ad.setCancelable(false); // This blocks the 'BACK' button
			ad.setMessage("Please enter a beer name and ABV");
			ad.setButton("OK", new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int which) {
			        dialog.dismiss();                    
			    }
			});
			ad.show();
		}
		else {
			
			String beerString = String.format("%s,%s,%s,,", breweryName, beerName, abv );
			dbHelper.AddNewBeerToList(beerString);
			super.finish();
		}
	}

}
